package cn.itcast.Servlet;

import cn.itcast.domain.Cake;
import cn.itcast.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/jiezhang")
public class jiezhang extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        //获取总价，购物车，清空购物车并且结账
        int totalprice= (int) session.getAttribute("totalprice");
        List<Cake> cart = (List) session.getAttribute("cart");
        User user = (User) session.getAttribute("user");
        if (user==null){

        }
        else if ((user.getYue()-totalprice)<0)
        {
            session.setAttribute("flag",true);
            response.sendRedirect(request.getContextPath()+"/carts.jsp");
        }
        else {
            user.setYue(user.getYue()-totalprice);
            session.setAttribute("totalprice",0);
            cart.clear();
            response.sendRedirect(request.getContextPath()+"/shopingcenter.jsp");

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
